package com.elite.groovy.dev;

import com.elite.groovy.kh.KHDYNUtil;
import com.elite.groovy.kh.KHUtil;
import com.elite.groovy.kh.TxConfigDev;
import com.elite.groovy.util.Util;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * @projectName: ngsapitask-cdcs
 * @package: com.elite.groovy.kh
 * @className: Test
 * @author: LiJT
 * @description: TODO
 * @date: 2023-05-05 20:17
 * @version: 1.0
 */
public class TestSyncGWPCA {
    private static final Log log = LogFactory.getLog(TestSyncGWPCA.class);

    private static String noStep_CityList = "文昌市\n" + "神农架林区\n" + "昌江黎族自治县\n" + "天门市\n" + "定安县\n" + "五指山市\n" + "仙桃市\n" + "保亭黎族苗族自治县\n" + "万宁市\n" + "潜江市\n" + "琼中黎族苗族自治县\n" + "屯昌县\n" + "东方市\n" + "琼海市\n" + "临高县\n" + "济源市\n" + "乐东黎族自治县\n" + "陵水黎族自治县\n" + "石河子市";

    public static void main(String[] args) throws Exception {


        JdbcTemplate jt = TxConfigDev.getJdbcTemplate();

        jt.update("delete from c_City_Err");
        jt.update("delete from c_Area_Err");
        List<Map<String, Object>> provinceList = jt.queryForList("select GW_PROVINCE_DESC from c_province where GW_PROVINCE_DESC is not null and GW_PROVINCE_DESC != '' and province_id in ('410000','420000','460000','650000')");
        log.debug("provinceList:" + provinceList);

        for (int i = 0; i < provinceList.size(); i++) {
            String provinceName = provinceList.get(i).get("GW_PROVINCE_DESC").toString();

            dealCity(jt, provinceName);


        }

    }

    // 处理城市
    public static String dealCity(JdbcTemplate jt, String provinceName) throws Exception {
        log.info("ProvinceName参数信息:" + provinceName);

        Map<String, String> headMap = new HashMap<String, String>();
        headMap.put("Content-Type", "application/x-www-form-urlencoded");
        log.debug("headMap参数信息:" + headMap);

        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("sender_prov", provinceName);
        log.debug("paramMap参数信息:" + paramMap);

        String injson = SplicingMap(paramMap, "&");
        log.debug("injson参数信息:" + injson);

        String cityInfo = Util.doHttpPostUseHead("https://stores.davco.cn/ajax.php?canshu=getcity", injson, "UTF-8", 12000, 0, headMap);
        log.debug("cityInfo信息:" + cityInfo);

        List<String> key_CNLists = KHDYNUtil.doListDist(KHDYNUtil.getzh_CNKeys(cityInfo, "([\\u4e00-\\u9fa5|0-9]+)"));
        log.info("CityInfo信息 Lists:" + key_CNLists);

        for (int i = 0; i < key_CNLists.size(); i++) {
            String CityName = key_CNLists.get(i);

            if (noStep_CityList.contains(CityName)) {
                List<Map<String, Object>> cityList = jt.queryForList("SELECT CODE FROM c_city WHERE CITY_DESC = ? or FIND_IN_SET(?,GW_CITY_DESC)", CityName, CityName);
                if (cityList.size() > 0) {
                    jt.update("update c_city set GW_CITY_DESC = if(FIND_IN_SET(?,GW_CITY_DESC),GW_CITY_DESC,concat(GW_CITY_DESC,',',?)) WHERE CODE = ?", CityName, CityName, cityList.get(0).get("CODE").toString());
                } else {
                    log.error("未找到 城市映射:" + CityName);
                    jt.update("Insert Into c_City_Err (Code, City_Id, City_Desc, Province_Id, Gw_City_Desc) Values ('', ?, '', '', ?)", "#" + KHUtil.getRandomString(5), CityName);
                }
                dealArea(jt, CityName);
            } else {
                log.debug("已经同步的城市直接跳过:" + CityName);
            }
        }

        return "";
    }


    // 处理区县
    public static String dealArea(JdbcTemplate jt, String CityName) throws Exception {
        log.info("CityName参数信息:" + CityName);

        Map<String, String> headMap = new HashMap<String, String>();
        headMap.put("Content-Type", "application/x-www-form-urlencoded");
        log.debug("headMap参数信息:" + headMap);

        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("sender_city", CityName);
        log.debug("paramMap参数信息:" + paramMap);

        String injson = SplicingMap(paramMap, "&");
        log.debug("injson参数信息:" + injson);

        String cityInfo = Util.doHttpPostUseHead("https://stores.davco.cn/ajax.php?canshu=getcounty", injson, "UTF-8", 12000, 0, headMap);
        log.debug("areaInfo信息:" + cityInfo);

        List<String> key_CNLists = KHDYNUtil.doListDist(KHDYNUtil.getzh_CNKeys(cityInfo, "([\\u4e00-\\u9fa5|0-9]+)"));
        log.info("AreaInfo信息 Lists:" + key_CNLists);

        for (int i = 0; i < key_CNLists.size(); i++) {
            List<Map<String, Object>> areaList = jt.queryForList("SELECT AREA_ID FROM c_AREA WHERE AREA_DESC = ?", key_CNLists.get(i));
            if (areaList.size() > 0) {
                jt.update("update c_AREA set GW_AREA_DESC = ? WHERE AREA_ID = ?", key_CNLists.get(i), areaList.get(0).get("AREA_ID").toString());
            } else {
                log.error("未找到 区县映射:" + key_CNLists.get(i));
                jt.update("Insert Into c_Area_Err (Area_Id, Area_Desc, Zipcode, City_Id, Province_Id, Gw_Area_Desc) Values (?, '', '', '', '', ?)", "#" + KHUtil.getRandomString(5), key_CNLists.get(i));
            }
        }

        return "";
    }

    // 参数处理
    public static String SplicingMap(Map<String, String> param, String Splicing) {
        TreeMap<String, String> paramTreeMap = new TreeMap<>(param);
        String prestr = "";
        for (Map.Entry<String, String> vo : paramTreeMap.entrySet()) {
            vo.getKey();
            vo.getValue();
            prestr = prestr + Splicing + vo.getKey() + "=" + vo.getValue();
        }
        prestr = prestr.substring(1);
        return prestr;
    }
}
